package com.approval.approvalapp.knowledge.zcfg;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.ZcFgBean;
import com.approval.approvalapp.knowledge.zcfg.adapter.ZcfgrvAdapter;
import com.approval.approvalapp.message.adapter.MessageListAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.CreateJsonUtil;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ZcfgFragment extends BaseFragment {


    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    private  int pagenum = 1;
    private ZcfgrvAdapter zcfgrvAdapter;
    private List<ZcFgBean> zcFgBeans;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_zcfg;
    }

    @Override
    public void initView(View view) {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        zcfgrvAdapter = new ZcfgrvAdapter();
        xrv.setAdapter(zcfgrvAdapter);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        },500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                addData(pagenum);
            }
        });
    }

    private void refreshData() {
        pagenum = 1;
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("page",1);
        HashMap<String , Object> hashMap = new HashMap<>();
        hashMap.put("page" , 1);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/data_list", CreateJsonUtil
                .creteJsonStr(hashMap), new NetDownResponse() {
            @Override
            public void success(String str) {
                zcFgBeans = JSON.parseArray(str, ZcFgBean.class);
                zcfgrvAdapter.setZcFgBeanList(zcFgBeans);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }
    private void addData(int pagenum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page",pagenum);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/data_list",jsonObject.toString() , new NetDownResponse() {
            @Override
            public void success(String str) {
                List<ZcFgBean> mzcFgBeans = JSON.parseArray(str, ZcFgBean.class);
                if (mzcFgBeans == null || mzcFgBeans .size() == 0) {
                    xrv.setNoMore(true);
                }else {
                    for (int i = 0; i < mzcFgBeans.size(); i++) {
                        zcFgBeans.add(mzcFgBeans.get(i));
                    }
                    zcfgrvAdapter.setZcFgBeanList(zcFgBeans);
                    xrv.loadMoreComplete();
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
                xrv.loadMoreComplete();
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
                xrv.loadMoreComplete();
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }


    @Override
    public void initData() {

    }


}
